package com.dd.reggie.controller;

import com.alibaba.druid.sql.visitor.functions.Now;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dd.reggie.common.BaseContext;
import com.dd.reggie.common.R;
import com.dd.reggie.entity.ShoppingCart;
import com.dd.reggie.entity.User;
import com.dd.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart, HttpSession session){

        log.info(shoppingCart.toString());

        //BaseContext通过拦截线程也能获得
        Long userId = (Long)session.getAttribute("user");

        //查询菜品/套餐是否已在购物车中
        shoppingCart.setUserId(userId);
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        //若添加的是菜品
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        //若添加的是套餐
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        //获取查询结果，看用户是否已选择此菜品/套餐
        ShoppingCart sc = shoppingCartService.getOne(queryWrapper);

        if(sc != null){
            sc.setNumber(sc.getNumber() + 1);
            sc.setCreateTime(LocalDateTime.now());
            shoppingCartService.updateById(sc);
            return R.success(sc);
        }
        else{
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }

    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){

        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId).orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){

        //虽然浏览器域名没写传入参数，但是通过负载可以发现菜品/套餐ID已经穿过来了
        log.info(shoppingCart.toString());

        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);

        //若为菜品
        queryWrapper.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());

        //若为套餐
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());

        ShoppingCart sc = shoppingCartService.getOne(queryWrapper);

        Integer number = sc.getNumber();
        if(number > 1){
            sc.setNumber(sc.getNumber() - 1);
            shoppingCartService.updateById(sc);
        }
        else{
            shoppingCartService.removeById(sc);
        }

        return R.success("减少成功");
    }

    @DeleteMapping("clean")
    public R<String> clean(){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);

        return R.success("清空成功");
    }

}
